#include "Console.h"
#include <vld.h>

console::console():isShowing(false),linePos(0),consoleChar(0), keyTimer(0.0f)
{

}

console::~console()
{
	release();
}

int console::addLine(char* _in)
{
	if(text.size() == MAX_LENGTH)
	{
		delete text.removeHeadNode();
		text.add(_in);
		return MAX_LENGTH;
	}

	text.add(_in);
	return text.size();
}

void console::addConsole(char* _in)
{
	addString(_in);
}

console & console::getInstance()
{
	static console instance;
	return instance;
}

void console::init(BeanieCore* a_core)
{
	m_core = a_core;
	ZeroMemory(textBuffer,LINE_LEN);
}

int console::addString(char* _in)
{

	char* newLine = new char[LINE_LEN+1];
	ZeroMemory(newLine,LINE_LEN+1);
	int lineCount = 0;
	int lineNumber = 0;
	for (int i = 0; i < strlen(_in); i++)
	{
		if((i%80)==0 && i!=0)
		{
			lineNumber = addLine(newLine);
			newLine = new char[LINE_LEN+1];
			ZeroMemory(newLine,LINE_LEN+1);
			lineCount++;
		}
		newLine[i%80] = _in[i];
	}
	if(strlen(newLine) > 0)
	{
		lineNumber = addLine(newLine);
		lineCount++;
	}
	else
		delete newLine;
	return lineNumber;
}

void console::release()
{
	int length = text.size();
	for (int i = 0; i < length; i++)
	{
		delete text.getFirst()->data;
		delete text.removeHeadNode();
	}
}

void console::draw()
{
	if(!isShowing)
		return;
	int height = 1280/8;
	for (int i = 0; i < height; i++)
	{
		//m_core->drawLine(V2DF(i*8+2,0.0f),V2DF(i*8+2,768.0f),0xFFFFFFFF);
	}
	//Main chat box
	//Left
	m_core->drawLine(V2DF(0.0f,0.0f),V2DF(0.0f,MAX_LENGTH*CHAR_HEIGHT),0xFFFFFFFF);
	//Bottom
	m_core->drawLine(V2DF(0.0f,MAX_LENGTH*CHAR_HEIGHT),V2DF((LINE_LEN*8),MAX_LENGTH*CHAR_HEIGHT),0xFFFFFFFF);
	//Right
	m_core->drawLine(V2DF((LINE_LEN*8)+2,0.0f),V2DF((LINE_LEN*8)+2,MAX_LENGTH*CHAR_HEIGHT),0xFFFFFFFF);
	//Top
	m_core->drawLine(V2DF(0.0f,0.0f),V2DF((LINE_LEN*8)+2,0.0f),0xFFFFFFFF);

	//Box to input text
	//Left
	m_core->drawLine(V2DF(0.0f,MAX_LENGTH*CHAR_HEIGHT),V2DF(0.0f,(MAX_LENGTH+1)*CHAR_HEIGHT),0xFFFFFFFF);
	//Bottom
	m_core->drawLine(V2DF(0.0f,(MAX_LENGTH+1)*CHAR_HEIGHT),V2DF((LINE_LEN*8),(MAX_LENGTH+1)*CHAR_HEIGHT),0xFFFFFFFF);
	//Right
	m_core->drawLine(V2DF((LINE_LEN*8)+2,MAX_LENGTH*CHAR_HEIGHT),V2DF((LINE_LEN*8)+2,(MAX_LENGTH+1)*CHAR_HEIGHT),0xFFFFFFFF);

	m_core->drawText(textBuffer,V2DF(2,MAX_LENGTH*CHAR_HEIGHT),0xFF880000);

	LLNode<char*>* currentNode;
	if(text.size())
	{
		currentNode= text.getFirst();
		//Find the starting position
		int length = text.size() -  MAX_LENGTH;
		for (int i = 0; i < length; i++)
		{
			currentNode = currentNode->next;
		}
	}
	for (int i = 0; i < MAX_LENGTH; i++)
	{
		if(currentNode == NULL)
			break;
		m_core->drawText(currentNode->data, V2DF(2.0f,CHAR_HEIGHT*i),0xFF0000FF);
		currentNode = currentNode->next;
	}

}

void console::toggleConsole()
{
	isShowing = !isShowing;
	ZeroMemory(textBuffer,LINE_LEN);
	consoleChar = 0;
}

void console::clearConsole()
{
	int length = text.size();
	for (int i = 0; i < length; i++)
	{
		delete text.getFirst()->data;
		delete text.removeHeadNode();
	}
}

void console::input()
{
	char ch = keyPress();
	if(ch)
	{
		switch (ch)
		{
		case 13:
			if(strlen(textBuffer))
			{
				addConsole(textBuffer);
				ZeroMemory(textBuffer,LINE_LEN);
				consoleChar = 0;
			}
			break;
		case 8:
			if(keyTimer < BAK_TIME)
			{
				keyTimer += m_core->getElapsed();
				break;
			}
			if(strlen(textBuffer))
			{
				consoleChar--;
				textBuffer[consoleChar] = 0;
			}
			keyTimer = 0;
			break;
		default:
			if(consoleChar >= 80)
				break;
			textBuffer[consoleChar] = ch;
			consoleChar++;
			break;
		}
	}
}

char console::keyPress()
{
	int length = 256;

	if(m_core->anyKey())
	{
		char ch = -1;
		bool isShifting = false;
		if(m_core->keyInput(DIK_RSHIFT)||m_core->keyInput(DIK_LSHIFT))
			isShifting = true;
		for (int i = 0; i < length; i++)
		{
			if(m_core->keyPress(DIK_A)){
				if(isShifting)
					ch = 'A';
				else
					ch =  'a';
			}

			if(m_core->keyPress(DIK_B)){
				if(isShifting)
					ch =  'B';
				else
					ch =  'b';
			}
			if(m_core->keyPress(DIK_C)){
				if(isShifting)
					ch =  'C';
				else
					ch =  'c';
			}
			if(m_core->keyPress(DIK_D)){
				if(isShifting)
					ch =  'D';
				else
					ch =  'd';
			}
			if(m_core->keyPress(DIK_E)){
				if(isShifting)
					ch =  'E';
				else
					ch =  'e';
			}
			if(m_core->keyPress(DIK_F)){
				if(isShifting)
					ch =  'F';
				else
					ch =  'f';
			}
			if(m_core->keyPress(DIK_G)){
				if(isShifting)
					ch =  'G';
				else
					ch =  'g';
			}
			if(m_core->keyPress(DIK_H)){
				if(isShifting)
					ch =  'H';
				else
					ch =  'h';
			}
			if(m_core->keyPress(DIK_I)){
				if(isShifting)
					ch =  'I';
				else
					ch =  'i';
			}
			if(m_core->keyPress(DIK_J)){
				if(isShifting)
					ch =  'J';
				else
					ch =  'j';
			}
			if(m_core->keyPress(DIK_K)){
				if(isShifting)
					ch =  'K';
				else
					ch =  'k';
			}
			if(m_core->keyPress(DIK_L)){
				if(isShifting)
					ch =  'L';
				else
					ch =  'l';
			}
			if(m_core->keyPress(DIK_M)){
				if(isShifting)
					ch =  'M';
				else
					ch =  'm';
			}
			if(m_core->keyPress(DIK_N)){
				if(isShifting)
					ch =  'N';
				else
					ch =  'n';
			}
			if(m_core->keyPress(DIK_O)){
				if(isShifting)
					ch =  'O';
				else
					ch =  'o';
			}
			if(m_core->keyPress(DIK_P)){
				if(isShifting)
					ch =  'P';
				else
					ch =  'p';
			}
			if(m_core->keyPress(DIK_Q)){
				if(isShifting)
					ch =  'Q';
				else
					ch =  'q';
			}
			if(m_core->keyPress(DIK_R)){
				if(isShifting)
					ch =  'R';
				else
					ch =  'r';
			}
			if(m_core->keyPress(DIK_S)){
				if(isShifting)
					ch =  'S';
				else
					ch =  's';
			}
			if(m_core->keyPress(DIK_T)){
				if(isShifting)
					ch =  'T';
				else
					ch =  't';
			}
			if(m_core->keyPress(DIK_U)){
				if(isShifting)
					ch =  'U';
				else
					ch =  'u';
			}
			if(m_core->keyPress(DIK_V)){
				if(isShifting)
					ch =  'V';
				else
					ch =  'v';
			}
			if(m_core->keyPress(DIK_W)){
				if(isShifting)
					ch =  'W';
				else
					ch =  'w';
			}
			if(m_core->keyPress(DIK_X)){
				if(isShifting)
					ch =  'X';
				else
					ch =  'x';
			}
			if(m_core->keyPress(DIK_Y)){
				if(isShifting)
					ch =  'Y';
				else
					ch =  'y';
			}
			if(m_core->keyPress(DIK_Z)){
				if(isShifting)
					ch =  'Z';
				else
					ch =  'z';
			}
			if(m_core->keyPress(DIK_0)){
				if(isShifting)
					ch =  ')';
				else
					ch =  '0';
			}
			if(m_core->keyPress(DIK_1)){
				if(isShifting)
					ch =  '!';
				else
					ch =  '1';
			}
			if(m_core->keyPress(DIK_2)){
				if(isShifting)
					ch =  '@';
				else
					ch =  '2';
			}
			if(m_core->keyPress(DIK_3)){
				if(isShifting)
					ch =  '#';
				else
					ch =  '3';
			}
			if(m_core->keyPress(DIK_4)){
				if(isShifting)
					ch =  '$';
				else
					ch =  '4';
			}
			if(m_core->keyPress(DIK_5)){
				if(isShifting)
					ch =  '%';
				else
					ch =  '5';
			}
			if(m_core->keyPress(DIK_6)){
				if(isShifting)
					ch =  '^';
				else
					ch =  '6';
			}
			if(m_core->keyPress(DIK_7)){
				if(isShifting)
					ch =  '&';
				else
					ch =  '7';
			}
			if(m_core->keyPress(DIK_8)){
				if(isShifting)
					ch =  '*';
				else
					ch =  '8';
			}
			if(m_core->keyPress(DIK_9)){
				if(isShifting)
					ch =  '(';
				else
					ch =  '9';
			}
			if(m_core->keyPress(DIK_MINUS)){
				if(isShifting)
					ch =  '_';
				else
					ch =  '-';
			}
			if(m_core->keyPress(DIK_EQUALS)){
				if(isShifting)
					ch =  '+';
				else
					ch =  '=';
			}
			if(m_core->keyInput(DIK_BACKSPACE)){
				ch =  8;
			}
			if(m_core->keyPress(DIK_BACKSLASH)){
				if(isShifting)
					ch =  '|';
				else
					ch =  '\\';
			}
			if(m_core->keyPress(DIK_SEMICOLON)){
				if(isShifting)
					ch =  ')';
				else
					ch =  ';';
			}
			if(m_core->keyPress(DIK_APOSTROPHE)){
				if(isShifting)
					ch =  '"';
				else
					ch =  '\'';
			}
			if(m_core->keyPress(DIK_SLASH)){
				if(isShifting)
					ch =  '?';
				else
					ch =  '/';
			}
			if(m_core->keyPress(DIK_COMMA)){
				if(isShifting)
					ch =  '<';
				else
					ch =  ',';
			}
			if(m_core->keyPress(DIK_PERIOD)){
				if(isShifting)
					ch =  '>';
				else
					ch =  '.';
			}
			if(m_core->keyPress(DIK_SPACE)){
				ch =  ' ';
			}
			if(m_core->keyPress(DIK_NUMPAD0)){
				ch =  '0';
			}
			if(m_core->keyPress(DIK_NUMPAD1)){
				ch =  '1';
			}
			if(m_core->keyPress(DIK_NUMPAD2)){
				ch =  '2';
			}
			if(m_core->keyPress(DIK_NUMPAD3)){
				ch =  '3';
			}
			if(m_core->keyPress(DIK_NUMPAD4)){
				ch =  '4';
			}
			if(m_core->keyPress(DIK_NUMPAD5)){
				ch =  '5';
			}
			if(m_core->keyPress(DIK_NUMPAD6)){
				ch =  '6';
			}
			if(m_core->keyPress(DIK_NUMPAD7)){
				ch =  '7';
			}
			if(m_core->keyPress(DIK_NUMPAD8)){
				ch =  '8';
			}
			if(m_core->keyPress(DIK_NUMPAD9)){
				ch =  '9';
			}
			if(m_core->keyPress(DIK_SUBTRACT)){
				ch =  '-';
			}
			if(m_core->keyPress(DIK_ADD)){
				ch =  '+';
			}
			if(m_core->keyPress(DIK_MULTIPLY)){
				ch =  '*';
			}
			if(m_core->keyPress(DIK_DIVIDE)){
				ch =  '/';
			}
			if(m_core->keyPress(DIK_NUMPADPERIOD)){
				ch =  '.';
			}
			if(m_core->keyPress(DIK_NUMPADENTER)){
				ch =  13;
			}
			if(m_core->keyPress(DIK_RETURN)){
				ch =  13;
			}
			if(ch != -1)
				return ch;
		}
		return 0;
	}
	return 0;
}